<?php

namespace app\api\controller;

use app\common\controller\Frontend;
use app\common\library\Utils;
use think\db\exception\PDOException;

class Order extends Frontend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = [];

    public function initialize(): void
    {
        parent::initialize();
    }

    /**
     * 下单
     * @return void
     */
    public function checkout(){
        $post_data = $this->request->post();
        $data = [
            'type'=> $post_data['type'],
            'product_no'=> $post_data['product_no'],
            'user_id' => $this->auth->id,
            'no' => Utils::createNo(),
            'num' => intval($post_data['num']),
            'product_category_id' => intval($post_data['category_id']),
            'img_position' => $post_data['img_position'],
            'color_remark' => $post_data['color_remark'],
            'tips' => $post_data['tips'],
            'images' => $post_data['images'],
            'remark' => $post_data['remark'],
        ];
        try {
            \app\common\model\Order::create($data);
            $this->success('下单成功!');
        } catch (PDOException $e){
            $this->error('下单失败!');
        }
    }

    public function lists(){
        $limit = $this->request->post('limit');
        $where = [];
        $res = \app\common\model\Order::with(['user','department','device','productCategory'])
            ->where($where)
            ->order('id desc')
            ->paginate($limit);
        $this->success('', [
            'list'   => $res->items(),
            'total'  => $res->total(),
            'remark' => get_route_remark(),
        ]);
    }

}